 window.console = (function() {
    if (window.console) {
        return window.console;
    } else {
        return {
            log: function() {}
        };
    }
})();

var browser= window.browser = this.browser ={
versions:function(){
        var u = navigator.userAgent, app = navigator.appVersion;
        return {        
            trident: u.indexOf('Trident') > -1, 
            presto: u.indexOf('Presto') > -1, 
            webKit: u.indexOf('AppleWebKit') > -1, 
            gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, 
            mobile: !!u.match(/AppleWebKit.*Mobile.*/), 
            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), 
            android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, 
            iPhone: u.indexOf('iPhone') > -1 , 
            iPad: u.indexOf('iPad') > -1, 
            webApp: u.indexOf('Safari') == -1 
        };
     }(),
     language:(navigator.browserLanguage || navigator.language).toLowerCase()
}

var CLOFOOD = window.CLOFOOD = this.CLOFOOD = {
	showalertmsg:function(msg,closetime,icon,callback){
        var widths = $(window).width();
        var heights = $(window).height();
        var html = "<div id='_jsshowmsg'  style='position:fixed; left:"+(widths/2)/2+"px; top:"+(heights/2-40)+"px; width:"+widths/2+"px; padding:0.25rem; background:#000; display:none; opacity:0.8; border-radius:0.2rem; z-index:999999999'>";
        if(typeof(icon)!='undefined')
            html+="<p style='text-align:center; padding:0.25rem 0;'><img src='/public/v3/img/main3/"+icon+".png' width='40' height='40' style='width:1rem;height:1rem;' /></p>";
        html+="<p style='text-align:center; color:#fff; padding:0.125rem 0;font-size:0.7rem'>"+msg+"</p></div>";
        $('body').append(html);
        $('#_jsshowmsg').fadeIn(200,function(){
            setTimeout(function(){
               $('#_jsshowmsg').fadeOut(200,function(){
                    $('#_jsshowmsg').remove();
                    if(typeof callback !='undefined'){
                        callback();
                    }
               }); 
            },closetime);
        })
	},
	openurl:function(url,backurl){
		if($('body').data('login')){
			if(url){
				window.location.href = url;
			}else{
				window.location.reload();
			}            
		}else{
			window.location.href = backurl?backurl:'index1.php';
		}
	},
    baseopengoodsurl:function(product_no,backurl,login){
        if($('body').data('login') && login){
            if($('body').data('appsource') == 'ios'){
                doAction('toGoodsDetail','{"productNo":"'+product_no+'"}','');
            }else if($('body').data('appsource') == 'android'){
                Yco.doAction('toGoodsDetail','{productNo:'+product_no+'}','');
            }else{
                window.location.href = 'index1.php?c=product&a=detail&product_no='+product_no;
            }
        }else{
            if(login){
                if($('body').data('appsource') == 'ios'){
                    doAction('toLogin','','');
                }else if($('body').data('appsource') == 'android'){
                    Yco.doAction('toLogin','','');
                }else{
                    window.location.href = backurl?backurl:'index1.php';
                }
            }else{
                if($('body').data('appsource') == 'ios'){
                    doAction('toGoodsDetail','{"productNo":"'+product_no+'"}','');
                }else if($('body').data('appsource') == 'android'){
                    Yco.doAction('toGoodsDetail','{productNo:'+product_no+'}','');
                }else{
                    window.location.href = 'index1.php?c=product&a=detail&product_no='+product_no;
                }
            }
        }
    },
    addbuycart:function(productno,count,activeid,callback){
          if(productno == ''){
              CLOFOOD.showalertmsg('错误的提交',2000,'no');
              return false;
          }
          $.post('index1.php?c=buycart&a=add',{
                'product_no':productno,
                'count':count,
                'activeid':activeid
          },function(data){
                if(data.status==1){
                    //CLOFOOD.showalertmsg('成功加入购物车',2000,'yes');
                    callback(productno,data.data.count);
                }else{
                    CLOFOOD.showalertmsg(data.msg,2000,'yes');
                }
          },'json');
          return true;
    },
    updatebuycart:function(productno,count,activeid,callback){
          if(productno == ''){
              CLOFOOD.showalertmsg('错误的提交',2000,'no');
              return false;
          }
          $.post('index1.php?c=buycart&a=update',{
                'product_no':productno,
                'count':count,
                'activeid':activeid
          },function(data){
                if(data.status==1){
                    //CLOFOOD.showalertmsg('成功加入购物车',2000,'yes');
                    callback(productno,count);
                }else{
                    CLOFOOD.showalertmsg(data.msg,2000,'yes');
                }
          },'json');
          return true;
    },
    isCookie:function(key,value,func,expireat){
    	if($.cookie(key) == value){
    		func();
    	}else
    	{
    		$.cookie(key,value,{expires:expireat})
    	}
    },
    setCookie:function(key,value){
    	$.cookie(key,value);
    	//store.set(key,value);
    },
    getCookie:function(key){
    	if($.cookie(key)!=undefined){
    		var temp = $.cookie(key);
    		return temp;
    	}
    	// if(store.get(key)!=null){
    	// 	var temp = store.get(key)
    	// 	return temp;
    	// }
    	return null;
    },
    removeCookie:function(key){
    	$.cookie(key, '', { expires: -1 });
    	//store.remove(key)
    },
    getAppType:function(){
        return clofood_app_type;
    },
    isAppType:function(){
        var apptype=CLOFOOD.getAppType();
        if(apptype=='android' || apptype=='ios'){
            return true;
        }
        return false;
    },
    apprun:function(action,dataobj){
        if(!CLOFOOD.isAppType()){
            return false;
        }
        if(typeof(dataobj)=='undefined'){
            dataobj={};
        }
        var type=CLOFOOD.getAppType();
        var str=JSON.stringify(dataobj);
        if(type=='android'){
            Yco.doAction(action,str,'');
        }
        else if(type=="ios"){
            //console.log(action+str);
            doAction(action,str,'');
        }
        // else if(type=='ios8'){
        //     dataobj['action']=action;
        //     window.webkit.messageHandlers.jsCallOC.postMessage(dataobj);
        // }
        else{
            return false;
        }
    },
    gourl:function(url,appaction,appdata){
        if(CLOFOOD.isAppType() && typeof(appaction) != 'undefined'){
            if(appaction=='openUrl'){
                if(typeof appdata == 'undefined'){
                    appdata={};
                }
                //判断各种原声-链接对应关系
                if(url.indexOf('c=product&a=run')!=-1){
                    appaction='productList';
                    var category='';
                    var children='';
                    var pos=url.indexOf('category=');
                    var temp=url.substring(pos+'category='.length);
                    pos=temp.indexOf('&');
                    if(pos==-1){
                        category=temp;
                    }
                    else{
                        category=temp.substring(0,pos);
                    }
                    if(url.indexOf('children=1')!=-1){
                        children=1;
                    }
                    appdata['category']=category;
                    appdata['children']=children;    
                }
                else{
                    appdata['url']=url;
                } 
            }
            else if(appaction=='toShopCart'){//app临时处理
                window.location=url;
            }
            else if(appaction=='productDetail'){//app临时处理
                appaction='openUrl';
                appdata['url']='index1.php?c=product&a=detail&product_no='+appdata['product_no'];
            }
            CLOFOOD.apprun(appaction,appdata);
        }
        else{
            window.location=url;
        }
    },
    gobuycart:function(products){
        var url='index1.php?c=buycart&a=run';
        if(typeof products !='undefined' && products!=''){
            url+='&products='+products;
        }
        CLOFOOD.gourl(url);
    },
    islogin:function(){
        if(goballogin=='1'){
            return true;
        }
        return false;
    }
};

//手机号码检测
function checkmobile(mob){
    if(mob==""){
        return false;
    }
    if(mob!=""){
        var reg = /^1[3|4|5|7|8][0-9]{9}$/;
        if(!reg.test(mob)){
          return false;
        }
    }
    return true;
}

//检查是一个6位的数字
function checkcode(intcode){
    if(intcode==""){
        return false;
    }
    if(intcode!=""){
        var reg = /^[0-9]{6}$/;
        if(!reg.test(intcode)){
          return false;
        }
    }
    return true;
}
//js模版解析
var Pagetpl=function(tpl,datas,format){
    var formattpl=function(tpl,data,format){
        return tpl.replace(/`(\w+)`/g, function(m1, m2) {
            if (!m2){
                return "";
            }
            else if(format && format[m2]){
                return format[m2](data);
            }
            else if(typeof(data[m2]) == 'undefined'){
                return "";
            }
            return data[m2];
        });
    }
    var arr=[];
    $.each(datas, function(i, o) {
        var html=tpl;
        html=formattpl(html,o,format);
        arr.push(html);  
    });  
    return arr.join('');  
}
//截取字符串
function SubSemname(value,maxlen){
    var len=value.replace(/[^\x00-\xff]/g, "**").length;
    var rs='';
    if(len>maxlen){
        rs=value.substring(0, maxlen/2-2);
    }
    else{
        return value;
    }
    return rs+'....';
}
//判断是否是购物车按钮触发的事件
function IsBuycartTrigger(event){
    return $(event.target).is('a');
}


//一些需要公共处理的方法
$(function(){
    //商品名称截取
    $(".sem_name").each(function(index, el) {
        var val=$(this).html();
        var len=$(this).data("length");
        val=SubSemname(val,len);
        $(this).html(val);
    });
    //页面加载弱提示
    if(load_tip_msg!=''){
        CLOFOOD.showalertmsg(load_tip_msg,load_tip_time);
    }
});